package com.tjetc.service.Impl;

import com.tjetc.dao.MerchantFinanceMapper;
import com.tjetc.entity.DailyBill;
import com.tjetc.entity.Order;
import com.tjetc.entity.view.DailyBillView;
import com.tjetc.entity.view.MerchantFinanceView;
import com.tjetc.service.MerchantFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
@Service
public class MerchantFinanceServiceImpl implements MerchantFinanceService {

    @Autowired
    MerchantFinanceMapper merchantFinanceMapper;


    @Override
    public MerchantFinanceView getAmountStatistic(Integer merchantId) {
        //季度列表，包含每个季度的开始一个月
        List<Integer> mounthBeginList=new ArrayList<>();
        mounthBeginList.add(1);
        mounthBeginList.add(4);
        mounthBeginList.add(7);
        mounthBeginList.add(10);
        MerchantFinanceView merchantFinanceView=new MerchantFinanceView();
        //搜索商家的账户余额
        BigDecimal amount=merchantFinanceMapper.selectMerchantAmount(merchantId);
        //如果搜索结果为空，注意设置为0
        if(amount==null){
            amount= BigDecimal.valueOf(0);
        }
        //当前月第一天
        LocalDate beginDate=LocalDate.now().withDayOfMonth(1);
        //今日
        LocalDate endDate=LocalDate.now();
        //从本月第一日到今日的营业额
        BigDecimal mouthAmount=merchantFinanceMapper.selectInDailyTable(merchantId,LocalDateTime.of(beginDate, LocalTime.MIN),LocalDateTime.of(endDate,LocalTime.MAX));
        if(mouthAmount==null){
            mouthAmount=BigDecimal.valueOf(0);
        }
        //计算上一个月的开始日期以及上个月的今天
        LocalDate lastBeginDate = beginDate.withMonth(beginDate.getMonthValue()-1);
        //使用Math.min取上个月的今天以及上个月的总天数的最小值，防止日期溢出
        LocalDate lastEndDate=lastBeginDate.plusDays(Math.min(ChronoUnit.DAYS.between(beginDate, endDate), YearMonth.of(lastBeginDate.getYear(),lastBeginDate.getMonthValue()).lengthOfMonth()-1));
        BigDecimal lastMouthAmount=merchantFinanceMapper.selectInDailyTable(merchantId,LocalDateTime.of(lastBeginDate,LocalTime.MIN),LocalDateTime.of(lastEndDate,LocalTime.MAX));
        //查询出上个月的营业额
        if(lastMouthAmount==null){
            lastMouthAmount=BigDecimal.valueOf(0);
        }
        //获得上一个季度的开头月份
        Integer mouthValue=(beginDate.getMonthValue())/3-2;
        LocalDate begin;
        LocalDate end;
        //计算出上个季度的第一天以及最后一天
        if (mouthValue<0){
            mouthValue=3;
            begin=LocalDate.of(beginDate.getYear()-1, mounthBeginList.get(mouthValue),1);
            end=LocalDate.of(beginDate.getYear()-1,mounthBeginList.get(mouthValue)+2,LocalDate.of(beginDate.getYear()-1,mounthBeginList.get(mouthValue)+2,1).getMonth().length(Year.isLeap(begin.getYear())));
        }else{
            begin=LocalDate.of(beginDate.getYear(), mounthBeginList.get(mouthValue),1);
            end=LocalDate.of(beginDate.getYear(),mounthBeginList.get(mouthValue)+2,LocalDate.of(beginDate.getYear(),mounthBeginList.get(mouthValue)+2,1).getMonth().length(Year.isLeap(begin.getYear())));
        }
        System.out.println(begin);
        System.out.println(end);
        //计算季度的营业额总和
        BigDecimal quarterAmount=merchantFinanceMapper.selectInMouthTable(merchantId,LocalDateTime.of(begin,LocalTime.MIN),LocalDateTime.of(end,LocalTime.MAX));
        if(quarterAmount==null){
            quarterAmount=BigDecimal.valueOf(0);
        }
        BigDecimal percent;
        //计算这个月相比上个月提升了多少个百分点，注意防止除0错误
        if(lastMouthAmount.equals(BigDecimal.valueOf(0))) {
            percent= BigDecimal.valueOf(0);
        }else{
            percent= (mouthAmount.subtract(lastMouthAmount)).divide(lastMouthAmount, 2, RoundingMode.HALF_UP);
        }
        merchantFinanceView.setAmount(amount);
        merchantFinanceView.setMouthAmount(mouthAmount);
        merchantFinanceView.setQuarterAmount(quarterAmount);
        merchantFinanceView.setMouthPercent(percent);
        return merchantFinanceView;
    }

    //获得指定时间内的账单
    @Override
    public DailyBillView getDailyBill(Integer merchantId, LocalDate beginDate, LocalDate endDate) {
        DailyBillView dailyBillView=new DailyBillView();
        List<LocalDate> dateList=new ArrayList<>();
        //查询一个月的账单列表
        List<DailyBill> dailyBills=merchantFinanceMapper.selectDailyBillByMerchantId(merchantId,LocalDateTime.of(beginDate,LocalTime.MIN),LocalDateTime.of(endDate,LocalTime.MAX));
        List<BigDecimal> amountList=new ArrayList<>();
        //初始化日期列表
        dateList.add(beginDate);
        while(!beginDate.equals(endDate)){
            beginDate=beginDate.plusDays(1);
            dateList.add(beginDate);
        }
        //将每日营业额存入营业额列表，便于画图
        for (DailyBill bill : dailyBills) {
            amountList.add(bill.getMerchantAmount());
        }
        dailyBillView.setDailyBills(dailyBills);
        dailyBillView.setDateList(dateList);
        dailyBillView.setAmountList(amountList);
        return dailyBillView;
    }

    //获得当天所有的订单
    @Override
    public List<Order> getOrdersByDay(Integer merchantId, LocalDate date) {
        LocalDateTime begin=LocalDateTime.of(date,LocalTime.MIN);
        LocalDateTime end=LocalDateTime.of(date,LocalTime.MAX);
        return merchantFinanceMapper.selectOrdersByTime(merchantId,begin,end);
    }
}
